package com.zhdl.network.modbustcp.after;

import cn.hutool.core.util.HexUtil;
import com.project.command.instruction.strategy.abs.AbsAfterControlCommand;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.project.common.util.ByteHelper;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.modules.autopick.service.CmdHandleService;
import com.zhdl.network.modbus.dto.ModbusDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(ModbusAfterService.MODBUS_TCP_AFTER_SERVICE)
public class ModbusAfterService extends AbsAfterControlCommand<ModbusDto, ByteBuf> {
    public static final String MODBUS_TCP_AFTER_SERVICE = "modbusTcpAfterService";
    /**
     * 1电机-状态1 2电机-状态2
     */
    public static String status1 = "00 00", status2 = "00 00";
    /**
     * 当前使用环境 -----此处还需加入使用环境的逻辑
     */
    public static String ENV_static = null;
    @Autowired
    private CmdHandleService cmdHandleService;

    /**
     * 后置处理结果判断
     *
     * @param resp
     * @return
     */
    @Override
    public boolean afterResultJudge(AfterStrategyParam resp) {
        log.debug("执行MOTOR_AFTER_SERVICE modbusAfterService");

        List<byte[]> bytes = resp.getNewData();
        byte[] bytes1 = bytes.get(0);
        String s = HexUtil.encodeHexStr(bytes1);
        System.out.println(s);
        ModbusDto modbusDto = new ModbusDto();
        String substring = s.substring(14);
//        String type = substring.substring(0,2);
        String instruction = resp.getInstruction().replaceAll(" ", "");
        String type = instruction.substring(14, 16);
        modbusDto.setCmdType((byte) Integer.parseInt(type));

        switch (type) {
            case "06", "05", "10", "0F" -> {
                modbusDto.setFullBuff(bytes1);
                modbusDto.setUnitBuff(ByteHelper.hexString2Bytes(substring));
            }
            case "01", "03" , "02" -> {
                modbusDto.setFullBuff(bytes1);
                modbusDto.setUnitBuff(ByteHelper.hexString2Bytes(substring.substring(4)));
            }
            default -> {
                System.out.println("功能码为：" + type + "，未匹配到对应的功能码");
            }
        }
        List<ModbusDto> temp = new ArrayList<>();
        temp.add(modbusDto);
        resp.setNewData(temp);
        EventBusCenter.getEventBus().post(resp);
        log.info("TCP" + resp.getPortKey() + "发送指令>>>>" + resp.getInstruction() + "<<<<<<<收到指令" + HexUtil.encodeHexStr(modbusDto.getFullBuff()));
        return true;
    }

    /**
     * 判断 读取 3200 状态字数据
     */
    public String getMotorStatus(ModbusDto modbusDto) {
        byte[] unitBuff = modbusDto.getUnitBuff();
        return HexUtil.encodeHexStr(unitBuff);
    }


    /*

//        List<ModbusDto> list = resp.getNewData();
//        //此处接收串口或网口客户端回应代码,并处理
//        ModbusDto motorDto = list.get(0);
//        String instruction = resp.getInstruction();
//        instruction = instruction.replace(" ", "");
//        if (instruction.equals(PublicTools.getString("cmd", "motorInfo"))) {
//            log.info("查询电机转速:hex " + HexUtil.encodeHexStr(motorDto.getUnitBuff()));
//            String s = HexUtil.encodeHexStr(motorDto.getUnitBuff());
//            Integer integer = Integer.parseInt(s, 16);
//            short i = integer.shortValue();
//            log.info("查询电机转速:dex " + i);
//        }
//        EventBusCenter.getEventBus().post(resp);
//        //cmdHandleService.getStatus(resp);
//        log.info(Thread.currentThread().getName() + "------------------");
//        log.info("COM" + resp.getPortKey() + "发送指令>>>>" + instruction + "<<<<<<<收到指令" + HexUtil.encodeHexStr(motorDto.getFullBuff()));

     */

}
